package com.mrobuy.jdvc.dao.impl;

import com.gavel.database.SQLExecutor;
import com.mrobuy.jdvc.dao.VCCateSalePropDao;
import com.mrobuy.jdvc.dao.VCItemDao;
import com.mrobuy.jdvc.entity.VCItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;

@Repository("vcItemDao")
public class VCItemDaoImpl extends  CommonDaoImpl implements VCItemDao {

    private static Logger LOG = LoggerFactory.getLogger(VCCateSalePropDao.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<VCItem> load(String productCode) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from VC_ITEM where PRODUCTCODE = ? order by  ORIGINALCODE ");
        try {
            List<VCItem> list = jdbcTemplate.query(sql.toString(), new Object[]{productCode}, new BeanPropertyRowMapper(VCItem.class));
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public void update(VCItem vcItem, boolean updateNull) {
        try {
            SQLExecutor.tempLateUpate(vcItem, jdbcTemplate, updateNull);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<VCItem> loadByWareId(String wareid) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from VC_ITEM where WAREID = ?  ");
        try {
            List<VCItem> list = jdbcTemplate.query(sql.toString(), new Object[]{wareid}, new BeanPropertyRowMapper(VCItem.class));
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public VCItem loadBySkuId(String skuid) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from VC_ITEM where SKUID = ?  ");
        try {
            VCItem record = (VCItem)jdbcTemplate.queryForObject(sql.toString(), new Object[]{skuid}, new BeanPropertyRowMapper(VCItem.class));
            return record;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return null;
    }
}
